package org.example.leetCode;

import java.util.ArrayList;
import java.util.List;

/**
 * @author Administrator
 */
public class LevelOrder {

    public List<List<Integer>> levelOrder(TreeNode root) {
        if (root == null) {
            return null;
        }
        List<List<Integer>> lists = new ArrayList<>();
        dfs(root, 0, lists);
        return lists;
    }

    private void dfs(TreeNode root, int i, List<List<Integer>> lists) {
        if (lists.size() < i) {
            lists.add(new ArrayList<Integer>());
        }
        List<Integer> list = lists.get(i);
        list.add(root.val);
        if (root.left != null) {
            dfs(root.left, i + 1, lists);
        }
        if (root.right != null) {
            dfs(root.right, i + 1, lists);
        }
    }


    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode(int x) {
            val = x;
        }
    }
}
